// 获取接口数据的文件，返回的都是promise对象
import {
  get, post
} from '../utils/request'
import {
  OpenID
} from '../utils/const'
import { getStorageSync } from './wechat'

const mpvuePlareform = 'wx'
const API_URL = 'https://test.youbaobao.xyz:18081'
const openId = getStorageSync('openId')

// 获取首页数据
export function getHomeData (params) {
  return get(`${API_URL}/book/home/v2`, params)
}
// 获取首页，为你推荐、免费阅读、当前最热的数据
export function getHomeHomeData (url) {
  return get(`${API_URL}/book/home/${url}/v2`)
}
export function getOpenIdApi (code) {
  return get(`${API_URL}/openId/get`, { appId: OpenID.APP_ID, code, secret: OpenID.APP_SECRET })
}
export function register (openId, userInfo) {
  //  加上userInfo就注册失败，目前猜测是接口问题：(我的用户名是特殊符号,猜测正确，改了用户名可以了)
  return post(`${API_URL}/user/register`, { openId, ...userInfo, platform: mpvuePlareform })
}
// 获取热门搜索词
export function getHotSearch () {
  return get(`${API_URL}/book/hot-search`)
}
// 关键词搜索，搜索框的搜索事件
export function getSearch (keyword, openId, page) {
  return get(`${API_URL}/book/search`, { keyword, openId, page })
}
// 根据图书filename获取具体图书信息
export function getBookData (fileName) {
  return get(`${API_URL}/book/detail`, { fileName, openId })
}
export function setBookRank (fileName, rank) {
  // 设置图书评分
  const openId = getStorageSync('openId')
  return get(`${API_URL}/book/rank/save`, { fileName, openId, rank })
}
export function getBookContents (fileName) {
  // 获取图书目录信息
  return get(`${API_URL}/book/contents`, { fileName })
}
export function getBookIsInShelf (fileName) {
  return get(`${API_URL}/book/shelf/get`, { fileName, openId })
}
// 加入书架
export function setSaveBookShelf (shelf) {
  // 通过json字符串传递数据
  return get(`${API_URL}/book/shelf/save`, { shelf: JSON.stringify(shelf) })
}
// 移出书架
export function setRemoveBookShelf (shelf) {
  // 通过json字符串传递数据
  return get(`${API_URL}/book/shelf/remove`, { shelf: JSON.stringify(shelf) })
}
// 获取列表页数据，搜索页上点击前三栏(分类，作者，出版社)，点击分类获取分类下的图书
export function getSearchList (params) {
  return get(`${API_URL}/book/search-list`, params)
}
// 获取所有图书分类
export function getCategoryList () {
  return get(`${API_URL}/book/category/list/v2`)
}
// 获取书架图书
export function getShelfList () {
  return get(`${API_URL}/book/shelf/get`, { openId })
}
// 获取用户加入天数readDay
export function getUserDay () {
  return get(`${API_URL}/user/day`, { openId })
}
